/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.views;

import com.insurance.cis.controllers.ChannelChooserController;
import com.insurance.cis.daos.ChannelDao;
import com.insurance.cis.models.Channel;
import com.insurance.cis.scos.ChannelSearchCondition;
import com.insurance.cis.utils.MessageDialogUtils;
import java.util.List;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author TruongLQ
 */
public class ChannelJDialogChooser extends javax.swing.JDialog {

    DefaultTableModel dtm;
    ChannelChooserController controller = new ChannelChooserController(this);
    ChannelDao channelDao = new ChannelDao();
    MainJFrameView mainJFrame;
    
    CampaignJDialogCreationView campaignJDialogView;
    CampaignJDialogUpdateView campaignJDialogUpdateView;
    JPanel fromJpnl;
    
    /**
     * Creates new form ChannelJDialogChooser
     */
    public ChannelJDialogChooser(java.awt.Frame parent, boolean modal, JPanel fromJpnl) {
        super(parent, modal);
        initComponents();
        loadTableMetaData();
        mainJFrame = (MainJFrameView) parent;
        this.fromJpnl = fromJpnl;
        this.setResizable(false);
        this.setLocationRelativeTo(getRootPane());
    }
    
    public ChannelJDialogChooser(CampaignJDialogCreationView parent, boolean modal) {
        super(parent, modal);
        initComponents();
        loadTableMetaData();
        campaignJDialogView = parent;
        this.setResizable(false);
        this.setLocationRelativeTo(getRootPane());
    }
    
    public ChannelJDialogChooser(CampaignJDialogUpdateView parent, boolean modal) {
        super(parent, modal);
        initComponents();
        loadTableMetaData();
        campaignJDialogUpdateView = parent;
        this.setResizable(false);
        this.setLocationRelativeTo(getRootPane());
    }

    private void loadTableMetaData() {
        Vector colHeader = new Vector(2);
        colHeader.add("Channel ID");
        colHeader.add("Channel Description");
        dtm = new DefaultTableModel(colHeader, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        tblChannelResult.setModel(dtm);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtChannelID = new javax.swing.JTextField();
        txtChannelDesc = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblChannelResult = new javax.swing.JTable();
        btnChooseItem = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Channel Chooser Dialog");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED), "Search criteria", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12)))); // NOI18N

        jLabel1.setText("Channel ID");

        jLabel2.setText("Channel Description");

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(txtChannelDesc, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(31, 31, 31)
                        .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(txtChannelID, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtChannelID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtChannelDesc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblChannelResult.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblChannelResult);

        btnChooseItem.setText("Choose being selected item");
        btnChooseItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChooseItemActionPerformed(evt);
            }
        });

        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(43, 43, 43)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnChooseItem)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addContainerGap(46, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnChooseItem)
                    .addComponent(btnClose))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        if (!controller.validateInputs()) {
            return;
        }
        List<Channel> channels = channelDao.getChannels(new ChannelSearchCondition(
                txtChannelID.getText().trim(), txtChannelDesc.getText().trim()));
        if (!channels.isEmpty()) {
            loadResults(channels);
        } else {
            MessageDialogUtils.dialogMessage(this, "No channels found");
            dtm.getDataVector().clear();
            tblChannelResult.updateUI();
        }
    }//GEN-LAST:event_btnSearchActionPerformed

    private void btnChooseItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChooseItemActionPerformed
        int row = tblChannelResult.getSelectedRow();
        if (row <0 || row >= tblChannelResult.getRowCount()) {
            MessageDialogUtils.dialogError(this, "You must choose one item to proceed!");
            return;
        }
        if (mainJFrame!= null) {
            if (fromJpnl instanceof CampaignMasterJPanelView) {
                mainJFrame.getCampaignMasterJpnl().getTxtChannelID().setText(
                dtm.getValueAt(tblChannelResult.getSelectedRow(), 0).toString());
            } else if (fromJpnl instanceof EstimatedCampaignJPanelView) {
                mainJFrame.getEstimatedCampaignJpnl().getTxtChannelID().setText(
                dtm.getValueAt(tblChannelResult.getSelectedRow(), 0).toString());
            } else if (fromJpnl instanceof ActualCampaignJPanelView) {
                mainJFrame.getActualCampaignJpnl().getTxtChannelID().setText(
                dtm.getValueAt(tblChannelResult.getSelectedRow(), 0).toString());
            }
            
        } else if (campaignJDialogView!=null) {
             campaignJDialogView.getTxtChannelID().setText(
                dtm.getValueAt(tblChannelResult.getSelectedRow(), 0).toString());
        } else if (campaignJDialogUpdateView!=null) {
             campaignJDialogUpdateView.getTxtChannelID().setText(
                dtm.getValueAt(tblChannelResult.getSelectedRow(), 0).toString());
        }
        
        this.dispose();
    }//GEN-LAST:event_btnChooseItemActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    private void loadResults(List<Channel> channels) {
        dtm.getDataVector().clear();
        for (Channel channel : channels) {
            dtm.addRow(new Object[]{channel.getChannelID(),
                        channel.getChannelDesc()});
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnChooseItem;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblChannelResult;
    private javax.swing.JTextField txtChannelDesc;
    private javax.swing.JTextField txtChannelID;
    // End of variables declaration//GEN-END:variables

    public JTextField getTxtChannelDesc() {
        return txtChannelDesc;
    }

    public JTextField getTxtChannelID() {
        return txtChannelID;
    }
}
